/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.funda.xmppserver;


import com.funda.xmppserver.mysql.DBManager;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;

/**
 *
 * @author kurinchi
 */
public class XMPPServer extends Thread{

    protected int PORT = 5222;
    protected ServerSocket serverSocket;
    
    protected Socket clientsock;

    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "funda";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "1234";

    XMPPClient client;

    DBManager dbconn;

    /**
     * Get the value of clientsock
     *
     * @return the value of clientsock
     */
    public Socket getClientsock() {
        return clientsock;
    }

    /**
     * Set the value of clientsock
     *
     * @param clientsock new value of clientsock
     */
    public void setClientsock(Socket clientsock) {
        this.clientsock = clientsock;
    }

    
    /**
     * Get the value of serverSocket
     *
     * @return the value of serverSocket
     */
    public ServerSocket getServerSocket() {
        return serverSocket;
    }

    /**
     * Set the value of serverSocket
     *
     * @param serverSocket new value of serverSocket
     */
    public void setServerSocket(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }


    /**
     * Get the value of PORT
     *
     * @return the value of PORT
     */
    public int getPORT() {
        return PORT;
    }

    /**
     * Set the value of PORT
     *
     * @param PORT new value of PORT
     */
    public void setPORT(int PORT) {
        this.PORT = PORT;
    }


    

    public XMPPServer() {
        try
        {
        serverSocket = new ServerSocket(PORT);
        //serverSocket.setSoTimeout(10000);
        InetAddress addr = InetAddress.getLocalHost();
        XMPPInfo.setHOST(addr.getHostName());
        XMPPInfo.setPORT(PORT);

        dbconn=new DBManager(conn, url, dbName, driver, userName, password);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void run() {
        while (true)
            try {
                System.out.println("Waiting for client on port "
                        + serverSocket.getLocalPort() + "...");
                clientsock = serverSocket.accept();
                client=new XMPPClient(clientsock);
                Thread clientth=client;
                clientth.start();
            }
            catch(Exception ex)
            {
                System.out.println("client Unavailable : "+client.getUsername());
                ex.printStackTrace();
            }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            Thread t = new XMPPServer();
            t.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
